<?
class logger{

	public $isLogError=false;

	/**
	 * @var integer - Constant: the maximum log file size.
	 */
	//const LOG_FILE_SIZE=200000;
	const LOG_FILE_SIZE=200000;

	public function __construct(){
		// call the construct
		$this->init();
	}
	
	public function init(){
	}
	
	public function getFullUrl(){
		$ssl=$_SERVER["HTTPS"];
		$ssl=empty($ssl)?'':($ssl=="on")?"s":"";
		$protocol=$this->strleft(strtolower($_SERVER["SERVER_PROTOCOL"]),"/").$ssl;
		$port=($_SERVER["SERVER_PORT"]=="80")?"":(":".$_SERVER["SERVER_PORT"]);
		return($protocol."://".$_SERVER['HTTP_HOST'].$port.$_SERVER['REQUEST_URI']);
	}

	public function strleft($s1,$s2){
		return substr($s1,0,strpos($s1,$s2));
	}

	public function record($data="",$logFile=NULL,$includeInfo=true){
		$result=false;
		$logFile=($logFile===NULL)?APP_LOG_FILE:$logFile;
		if(@filesize($logFile)>logger::LOG_FILE_SIZE&&$logFile==APP_LOG_FILE){
			//print("<h1>Log file ($logFile) is bigger than ".logger::LOG_FILE_SIZE." (".@filesize($logFile).")</h1>".chr(13));
			$this->newLog();
		}
		if($includeInfo){
			$data=date("Y/m/d H:i:s")."\t".$_SERVER['REMOTE_ADDR']."\t".$this->getFullUrl()."\t$data".chr(13);
		}
		#print("<p>Attempting to log data (file:$logFile,data:$data</p>");
		if(@file_put_contents($logFile,$data,FILE_APPEND)){
			$result=true;
		}else{
			if($_REQUEST["debug"]=="OK"){
				print("<p>Couldnt Log Data here: ".$logFile."</p>");
			}
		}
		return($result);
	}

	public function read($logFile=NULL){
		$result=false;
		$logFile=($logFile===NULL)?APP_LOG_FILE:$logFile;
		$result=@file_get_contents($logFile);
		return($result);
	}

	public function getFileNameWithoutExt($path=NULL){
		$result=false;
		$path=basename($path);
		$result=substr($path,0,strrpos($path,"."));
		return($result);
	}
	
	public function newLog($logFile=NULL){
		$result=false;

		$logFile=($logFile===NULL)?APP_LOG_FILE:$logFile;

		$dir_path=dirname($logFile);
		$app_file_base=$this->getFileNameWithoutExt($logFile);
		//print("<h1>Log File: $logFile</h1>".chr(13));
		//print("<h1>Log File Base: $app_file_base</h1>".chr(13));
		//print("<h1>Dir Path: $dir_path</h1>".chr(13));

		$log_c=0;

		if($handle=opendir($dir_path)){
			while(($file=readdir($handle))!==false){
				if($file<>"." AND $file<>".."){
					$fileNoExt=$this->getFileNameWithoutExt($file);
					if($app_file_base==substr($file,0,strlen($app_file_base)) && $app_file_base!=$fileNoExt){
						//Get the Log File Number
						//print("<h1>Existing Log: ".$fileNoExt."</h1>".chr(13));
						$log_number=substr($fileNoExt,strrpos($fileNoExt,"_")+1);
						//print("<h1>This Log Number: ".$log_number."</h1>".chr(13));
						if((int)$log_number>=(int)$log_c){
							$log_c=(int)$log_number+1;
						}
					}
				}
			}
		}

		$fileName=split("\.",$logFile);
		$fileName=$fileName[0]."_".$log_c.".log";
		//$fileName=$fileName[0]."_".rand(0,1000000).".log";

		#print("<h1>Log $logFile now Log Number: $fileName</h1>".chr(13));

		if(@rename($logFile,$fileName)){
			$result=$this->record("New Log File Created. Old log file called $fileName",$logFile);
			unlink($logFile);
			if(file_exists($logFile)){
				print("<h1>$logFile Still exists</h1>".chr(13));
			}
		}
		return($result);
	}
};
?>